<!DOCTYPE html>

<html lang='en'>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/react.development16.4.0.js"></script>
    <script src="./lib/react-dom.development16.4.0.js"></script>
    <script src="./lib/babel.min.js"></script>
</head>

<body>
    <div id="app">
    </div>
    <script type="text/babel">
        
         class React_Ajax extends React.Component {
            constructor(props) {
                super(props);
                this.ajax_fun = this.ajax_fun.bind(this);
                this.state={
                    list01:null
                }
            }
            
          componentDidMount() {
              this.ajax_fun();
          }
          
          ajax_fun(){
                let url = 'https://5b5e71c98e9f160014b88cc9.mockapi.io/api/v1/lists';
               //1.创建ajax异步请求对象
               var xhr = new XMLHttpRequest();
               //2.打开和后端连接

               xhr.open('GET',url,true);
               //3.发送ajax异步请求
               xhr.send();

               //4.接收ajax请求返回数据
               xhr.onreadystatechange = ()=>{
                   //xmlhttp.status==200 表示请求成功 xmlhttp.readyState==4 执行ajax 成功并返回数据
                if (xhr.readyState==4 && xhr.status==200)
                 {
                    //console.log(xhr);
                    let response = xhr.responseText;

                    this.setState({list01:JSON.parse(response)});
                 }
               }
            }

             render() {
                if(!this.state.list01){
                    return (
                        <div>
                            <h1>load.......</h1>
                        </div>
                    )
                }else{
                  let list = this.state.list01;
                    return (
                        <div>
                            <ul>
                              {
                                  list.map((item)=><li key={item.id}>{item.id}--{item.name}--{item.sex}</li>)
                              }
                            </ul>
                        </div>
                    )
                }
            }
        }
        

        ReactDOM.render(
            <React_Ajax/>,document.getElementById('app')
        );
    </script>
</body>

</html>